<template>
  <!-- 医护评分 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="患者身份证号" align="center" key="idcard" prop="idcard" v-if="columns[0].visible"/>
      <el-table-column label="就诊过程配合度评分" align="center" key="cooperationScore" prop="cooperationScore"
                       v-if="columns[1].visible"
      />
      <el-table-column label="补缴费配合度评分" align="center" key="paymentScore" prop="paymentScore"
                       v-if="columns[2].visible"
      />
      <el-table-column label="评分日期" align="center" key="evaluationDate" prop="evaluationDate"
                       v-if="columns[3].visible"
      />
      <el-table-column label="有效期" align="center" key="remark" prop="remark" v-if="columns[5].visible"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.userId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"


          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"


          >删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="患者" prop="idcard">
         <!--  <el-select
            v-model="form.idcard"
            placeholder="请选择患者"
          >
            <el-option v-for="item in selectList"
                       :key="item.idcard"
                       :label="item.recorder"
                       :value="item.idcard"
            />
          </el-select> -->
          <el-input v-model="form.idcard" placeholder="患者" maxlength="30"/>
        </el-form-item>

<!--        <el-form-item label="角色">-->
<!--          <el-radio-group v-model="scoreType">-->
<!--            <el-radio label="1">护士评价</el-radio>-->
<!--            <el-radio label="2">催缴费人员评价</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->

        <el-form-item label="就诊过程配合度评分" prop="cooperationScore" v-if="isYH">
          <el-input v-model="form.cooperationScore" placeholder="请输入就诊过程配合度评分" maxlength="30"/>
        </el-form-item>
        <el-form-item label="补缴费配合度评分" prop="paymentScore" v-if="isCJ">
          <el-input v-model="form.paymentScore" placeholder="请输入补缴费配合度评分" maxlength="30"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="120"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { searchList, delUser, addUser, updateUser, selectPatient } from '@/api/credit/score'
import { checkRole } from '@/utils/permission'

export default {
  components: {},
  name: 'patient',
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idcard: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `患者身份证号`, visible: true },
        { key: 1, label: `身份类型`, visible: true },
        { key: 2, label: `评分`, visible: true },
        { key: 3, label: `记录日期`, visible: true },
        { key: 4, label: `登记人`, visible: true },
        { key: 5, label: `有效期`, visible: true }
      ],
      // 表单校验
      rules: {
        idcard: [
          { required: true, message: '证件号不能为空', trigger: 'blur' }
        ],
        cooperationScore: [
          { required: true, message: '就诊过程配合度评分不能为空', trigger: 'blur' }
        ],
        paymentScore: [
          { required: true, message: '补缴费配合度评分不能为空', trigger: 'blur' }
        ]
      },
      //患者列表
      selectList: [],
      //评价角色
      scoreType: '1', //1 护士  2催缴费人员
      isYH:false,// 是否为医护
      isCJ:false,// 是否为催缴费
    }
  },
  created() {
    this.getList()
    this.selectPatient()
    this.isYH=checkRole(['YH','admin'])// 是否为医护
    this.isCJ=checkRole(['CJF','admin'])// 是否为催缴费
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      searchList(this.queryParams).then(response => {
          this.userList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        idcard: undefined,
        cooperationScore: undefined,
        paymentScore: undefined,
        remark: undefined

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置搜索条件按钮操作 */
    resetQuery() {

      this.resetForm('queryParams')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加评分'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.idcard = row.idcard
      this.form.cooperationScore = row.cooperationScore
      this.form.paymentScore = row.paymentScore
      this.form.remark = row.remark
      this.form.id = row.id

      this.open = true
      this.title = '修改评分'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids
      console.log('userIds', userIds)
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser({ ids: userIds.toString() })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    //查询患者
    selectPatient() {
      selectPatient({}).then(response => {
          this.selectList = response.rows

        }
      )
    }

  }
}
</script>
